#define _CRT_SECURE_NO_WARNINGS 1


#include<stdio.h>
#include<stdlib.h>

struct ListNode
{
	int val;
	struct ListNode* next;
};


struct ListNode* ReverseGroup(struct ListNode* head, int k)
{
   {
        // write code here
        if (head == NULL)
        {
            return NULL;
        }
        struct ListNode* GuardHead = (struct ListNode*)malloc(sizeof(struct ListNode));
        GuardHead->next = head;
        struct ListNode* cur = GuardHead;
        struct ListNode* next = head;

        struct ListNode* tail = head;
        int n = 0;
        while (tail)
        {
            n++;
            tail = tail->next;
        }
        int count = n / k;
        for (int i = 0; i < count; i++)
        {
         
            for (int j = 0; j < k - 1; j++)
            {
                struct ListNode* nnext = next->next;
                next->next = nnext->next;
                nnext->next = cur->next;
                cur->next = nnext;
            }

            for (int j = 0; j < k; j++)
            {
                cur = cur->next;
            }
            next = cur->next;
        }
        head = GuardHead->next;
        free(GuardHead);
        GuardHead = NULL;
        return head;
    }
}

void Print(struct ListNode* head)
{
    struct ListNode* tail = head;
    while (tail)
    {
        printf("%d->", tail->val);
        tail = tail->next;
    }
    printf("NULL\n");
}

int main()
{
	struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n1->val = 1;
	struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n2->val = 2;
	struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n3->val = 3;
	struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n4->val = 4;
	struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n5->val = 5;
	struct ListNode* n6 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n6->val = 6;

	n1->next = n2;
	n2->next = n3;
	n3->next = n4;;
	n4->next = n5;
    n5->next = n6;
    n6->next = NULL;

	struct ListNode* head=ReverseGroup(n1, 3);

    Print(head);

	return 0;
}